package cn.tedu._02notice.controller;

import cn.tedu._02notice.base.response.JsonResult;
import cn.tedu._02notice.base.response.StatusCode;
import cn.tedu._02notice.mapper.NoticeMapper;
import cn.tedu._02notice.pojo.dto.NoticeAddParam;
import cn.tedu._02notice.pojo.dto.NoticeListQuery;
import cn.tedu._02notice.pojo.dto.NoticeUpdateParam;
import cn.tedu._02notice.pojo.entity.Notice;
import cn.tedu._02notice.pojo.vo.NoticeDetailInfoVO;
import cn.tedu._02notice.pojo.vo.NoticeListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@Tag(name = "01-资讯管理模块")
@RequestMapping("/v1/notice/")
public class NoticeController {
    @Autowired
    private NoticeMapper noticeMapper;

    @PostMapping("add")
    @Operation(summary = "添加资讯")
    @ApiOperationSupport(order = 10)
    @Parameters({
            @Parameter(name = "title", description = "资讯标题", required = true),
            @Parameter(name = "content", description = "资讯内容", required = true),
            @Parameter(name = "type", description = "资讯类型", required = true),
            @Parameter(name = "status", description = "资讯状态", required = true),
            @Parameter(name = "noticeAddParam",hidden = true)
    })
    public JsonResult addNotice(NoticeAddParam noticeAddParam) {
        log.debug("添加资讯：" + noticeAddParam);

        Notice notice = new Notice();

        //复制属性
        BeanUtils.copyProperties(noticeAddParam, notice);

        notice.setUserId(1L);
        notice.setCreateTime(new Date());
        notice.setUpdateTime(new Date());

        log.debug("准备插入数据库:" + notice);
        int rows = noticeMapper.insertNotice(notice);

        return JsonResult.ok();
    }

    @GetMapping("list")
    @Operation(summary = "查询资讯列表")
    @ApiOperationSupport(order = 20)
    public JsonResult listNotice(NoticeListQuery noticeListQuery) {
        log.debug("noticeListQuery = " + noticeListQuery);

        List<NoticeListVO> noticeListVOS = noticeMapper.selectNoticeList(noticeListQuery);

        return JsonResult.ok(noticeListVOS);
    }

    @GetMapping("detail")
    @Operation(summary = "查询资讯详情")
    @ApiOperationSupport(order = 30)
    @Parameter(name = "id", description = "资讯ID", required = true)
    public JsonResult detailInfoVO(Long id) {
        log.debug("id = " + id);

        NoticeDetailInfoVO noticeDetailInfoVO = noticeMapper.detailNotice(id);

        return JsonResult.ok(noticeDetailInfoVO);
    }

    @PostMapping("delete")
    @Operation(summary = "删除资讯")
    @ApiOperationSupport(order = 40)
    public JsonResult deleteNotice(Long id) {
        log.debug("id = " + id);

        int rows = noticeMapper.deleteNotice(id);
        return JsonResult.ok();
    }

    @PostMapping("update")
    @Operation(summary = "修改资讯")
    @ApiOperationSupport(order = 50)
    public JsonResult updateNotice(@RequestBody NoticeUpdateParam noticeUpdateParam) {
        log.debug("noticeUpdateParam = " + noticeUpdateParam);

        noticeUpdateParam.setUpdateTime(new Date());

        int raw = noticeMapper.updateNotice(noticeUpdateParam);
        if (raw > 0) {
            return JsonResult.ok();
        }
        return new JsonResult( StatusCode.POERATION_FAILED);
    }


}
